package cc.iotkit.data.service.impl;

import cc.iotkit.common.api.Paging;
import cc.iotkit.common.utils.MapstructUtils;
import cc.iotkit.data.mapper.ProtocolConverterMapper;
import cc.iotkit.data.model.TbProtocolConverter;
import cc.iotkit.data.service.ProtocolConverterService;
import cc.iotkit.model.protocol.ProtocolConverter;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: 石恒
 * @Date: 2023/7/26 23:02
 * @Description:
 */
@Service
@Primary
public class ProtocolConverterServiceImpl extends ServiceImpl<ProtocolConverterMapper, TbProtocolConverter> implements ProtocolConverterService {

    @Override
    public List<ProtocolConverter> findByUid(String uid) {
        List<TbProtocolConverter> protocolComponentList = this.baseMapper.selectList(new LambdaQueryWrapper<TbProtocolConverter>()
                .eq(StringUtils.isNotBlank(uid), TbProtocolConverter::getUid, uid));
        return MapstructUtils.convert(protocolComponentList, ProtocolConverter.class);
    }

    @Override
    public Paging<ProtocolConverter> findByUid(String uid, int page, int size) {
        Page<TbProtocolConverter> rowPage = new Page<>(page, size);
        Page<TbProtocolConverter> deviceGroupPage = this.baseMapper.selectPage(rowPage, new LambdaQueryWrapper<TbProtocolConverter>().eq(TbProtocolConverter::getUid, uid));
        Paging<ProtocolConverter> paging = new Paging<>();
        paging.setRows(MapstructUtils.convert(deviceGroupPage.getRecords(), ProtocolConverter.class));
        paging.setTotal(rowPage.getTotal());
        return paging;
    }

    @Override
    public long countByUid(String uid) {
        return this.baseMapper.selectCount(new LambdaQueryWrapper<TbProtocolConverter>()
                .eq(StringUtils.isNotBlank(uid), TbProtocolConverter::getUid, uid));
    }
}
